package controllers.system;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Config;
import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class ConfigController extends ApplicationController {

	public void index() throws Exception {

	}

	public View getConfigsItems(int page, int rows, String q, int t,
			String sort, String order) throws Exception {
		String orderBy = "id";
		if (sort != null && order != null) {
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();

		/**
		 * if (t != 0){ cond += " and (parentId = ?)"; tmpArgs.add(t); }
		 **/
		if (q != null) {
			cond += " and (cfgtype like '%" + q + "%' or cfgcode like '%" + q + "%' or cfgkeyname like '%"
					+ q + "%' ) ";
		}

		Object[] args = tmpArgs.toArray();

		long total = Config.count(Config.class, cond, args);
		List<Config> items = Config.findAll(Config.class, cond, args,
				orderBy, rows, (page - 1) * rows);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", items);

		return new JsonView(result);
	}

	public View saveConfigs(Config item, String[] rids) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		// 其他需要系统自动填写的字段
		// 其他操作如写入日志
		if (item.save()) {
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建配置，内容为：" + new JsonView(item));									
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}

		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View updateConfigs(int id, Map<String, Object> params, String[] rids)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		Config item = Config.find(Config.class, id);
		JsonView olditem= new JsonView(item);
		item = Config.updateModel(item, params);
		// 其他需要系统自动填写的字段
		// 其他操作如写入日志
		if (item.save()) {
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改配置，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}

		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}

	public View destroyConfigs(int id) throws Exception {
		Config item = Config.find(Config.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除配置，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}

}
